// Snow Animation
function createSnowflake() {
    const snowContainer = document.getElementById('snow-container');
    if (!snowContainer) return;
    
    const snowflake = document.createElement('div');
    snowflake.classList.add('snowflake');
    snowflake.textContent = '❄';
    snowflake.style.left = Math.random() * 100 + '%';
    snowflake.style.animationDuration = Math.random() * 3 + 5 + 's';
    snowflake.style.opacity = Math.random() * 0.6 + 0.4;
    snowflake.style.fontSize = Math.random() * 10 + 10 + 'px';
    
    snowContainer.appendChild(snowflake);
    
    setTimeout(() => {
        snowflake.remove();
    }, 8000);
}

// Create snowflakes periodically
setInterval(createSnowflake, 200);

// Audio Management
let bellSound = null;
let clickSound = null;
let imageButtons = null;
let audioInitialized = false;
let lastBellTime = 0;
let lastClickTime = 0;
const bellCooldown = 500;
const clickCooldown = 300;

// Initialize audio after DOM is fully loaded
function initAudio() {
    console.log('Initializing audio...');
    
    bellSound = document.getElementById('bellSound');
    clickSound = document.getElementById('clickSound');
    imageButtons = document.querySelectorAll('.image-button');
    
    if (!bellSound || !clickSound) {
        console.error('Audio elements not found!');
        return;
    }
    
    // Set volumes
    bellSound.volume = 0.3;
    clickSound.volume = 0.35;
    
    console.log('Audio elements initialized');
    console.log('Bell sound source:', bellSound.src);
    console.log('Click sound source:', clickSound.src);
    
    // Add error handlers
    bellSound.addEventListener('error', (e) => {
        console.error('Bell sound error:', e);
        console.error('Error details:', bellSound.error);
    });
    
    clickSound.addEventListener('error', (e) => {
        console.error('Click sound error:', e);
        console.error('Error details:', clickSound.error);
    });
    
    // Add loaded handlers
    bellSound.addEventListener('canplaythrough', () => {
        console.log('Bell sound loaded and ready');
    });
    
    clickSound.addEventListener('canplaythrough', () => {
        console.log('Click sound loaded and ready');
    });
    
    // Button hover sound effects
    imageButtons.forEach((button, index) => {
        button.addEventListener('mouseenter', () => {
            playBellSound();
        });
        
        // Button click sound effects
        button.addEventListener('click', (e) => {
            playClickSound();
        });
    });
    
    audioInitialized = true;
    console.log('Audio initialization complete');
}

// Play bell sound on hover
function playBellSound() {
    if (!bellSound) {
        console.error('Bell sound element not found');
        return;
    }
    
    const currentTime = Date.now();
    
    if (currentTime - lastBellTime > bellCooldown) {
        console.log('Playing bell sound');
        bellSound.currentTime = 0;
        const playPromise = bellSound.play();
        
        if (playPromise !== undefined) {
            playPromise.then(() => {
                console.log('Bell sound played successfully');
            }).catch(error => {
                console.error('Failed to play bell sound:', error);
            });
        }
        
        lastBellTime = currentTime;
    }
}

// Play click sound on button click
function playClickSound() {
    if (!clickSound) {
        console.error('Click sound element not found');
        return;
    }
    
    const currentTime = Date.now();
    
    if (currentTime - lastClickTime > clickCooldown) {
        console.log('Playing click sound');
        clickSound.currentTime = 0;
        const playPromise = clickSound.play();
        
        if (playPromise !== undefined) {
            playPromise.then(() => {
                console.log('Click sound played successfully');
            }).catch(error => {
                console.error('Failed to play click sound:', error);
            });
        }
        
        lastClickTime = currentTime;
    }
}

// Initialize on DOM ready
if (document.readyState === 'loading') {
    document.addEventListener('DOMContentLoaded', () => {
        console.log('DOM loaded');
        initAudio();
    });
} else {
    console.log('DOM already loaded');
    initAudio();
}

console.log('Script loaded');